<?php
	$xtpl_main->assign ( "page_title", "Project" );
	
	//-- Remove Task
	if(isset($_POST['taskId']) && $_POST['taskId'] != ''){
		mysql_query("
						DELETE FROM task WHERE task_id = '".$_POST['taskId']."' AND task_assigned_to = '".$_SESSION['onlineMemberId']."'
					");
		mysql_query("
						DELETE FROM report WHERE task_id = '".$_POST['taskId']."' AND member_id = '".$_SESSION['onlineMemberId']."'
					");
		header("Location: ?act=project&id=".$_POST['projectId']);
	}
	
	//--
	if(isset($_POST['frmAct']) && $_POST['frmAct'] == 'doReport'){
		$_POST['task_description'] = str_replace("\r", '', $_POST['task_description']);
		$_POST['task_description'] = preg_replace('/(?<!>)\n/', "<br />\n", $_POST['task_description']);
		if($_POST['frmTaskId'] == ''){
			mysql_query("
							INSERT INTO task 
								(project_id, task_assigned_to, task_created_date, task_description) 
							VALUES 
								('".$_POST['project_id']."', '".$_SESSION['onlineMemberId']."', '".date('Y-m-d')."', '".$_POST['task_description']."')
						");
			
			$taskId = mysql_insert_id();
			
			mysql_query("
							INSERT INTO report 
								(member_id, project_id, task_id, report_time, report_created_date) 
							VALUES 
								('".$_SESSION['onlineMemberId']."', '".$_POST['project_id']."', '".$taskId."', '".$_POST['task_time']."', '".date('Y-m-d')."')
						");
		}else{
			
			mysql_query("
							UPDATE task 
							SET 
								project_id = '".$_POST['project_id']."',
								task_description = '".$_POST['task_description']."' 
							WHERE 
								task_id = '".$_POST['frmTaskId']."' AND task_assigned_to = '".$_SESSION['onlineMemberId']."' 
							LIMIT 1
						");
			mysql_query("
							UPDATE report 
							SET 
								project_id = '".$_POST['project_id']."', 
								report_time = '".$_POST['task_time']."' 
							WHERE 
								task_id = '".$_POST['frmTaskId']."' AND member_id = '".$_SESSION['onlineMemberId']."'
						");
		}
		header("Location: ?act=project&id=".$_POST['project_id']);
	}
	
	//-- Select project
	if(isset($_GET['t']) && $_GET['t'] != ''){
		$rsSelectedProjectId = mysql_query("SELECT t.project_id, t.task_description, r.report_time, t.task_id FROM task t INNER JOIN report r ON r.task_id = t.task_id WHERE t.task_id = '".$_GET['t']."' AND t.task_assigned_to = '".$_SESSION['onlineMemberId']."'");
		if(mysql_num_rows($rsSelectedProjectId) > 0){
			$rowSelectedProjectId = mysql_fetch_array($rsSelectedProjectId);
			$selectedProjectId = $rowSelectedProjectId['project_id'];
			
			$xtpl->assign("editReportDescription", $rowSelectedProjectId['task_description']);
			$xtpl->assign("editReportTime", $rowSelectedProjectId['report_time']);
			$xtpl->assign("TaskId", $rowSelectedProjectId['task_id']);
		}
	}
	
	//-- List select option project
	$rsSelectProject = mysql_query("SELECT project_id, project_name FROM project WHERE 1 ORDER BY project_id DESC");
	while($rowSelectProject = mysql_fetch_array($rsSelectProject)){
		if(isset($selectedProjectId) && $selectedProjectId != '' && $rowSelectProject['project_id'] == $selectedProjectId) $rowSelectProject['project_selected'] = 'selected="selected"';
		$xtpl->assign("listSelectProject", $rowSelectProject);
		$xtpl->parse("center.listSelectProject");
	}
	
	//-- Project with task
	if(isset($_GET['id'])){
		$_GET['id'] = intval($_GET['id']);
	
		$rsListProjectWithTask = mysql_query("SELECT project_id, project_name FROM project WHERE project_id = '".$_GET['id']."' LIMIT 1");
		$rowListProjectWithTask = mysql_fetch_array($rsListProjectWithTask);
		
		$rsListTasks = mysql_query("
									SELECT t.*, m.member_displayname FROM task t 
									INNER JOIN member m ON m.member_id = t.task_assigned_to 
									WHERE t.project_id = '".$rowListProjectWithTask['project_id']."' 
									ORDER BY t.task_id DESC
								   ");
		$countTask = mysql_num_rows($rsListTasks);
		if($countTask > 0){
			$iCountTask = $countTask;
			while($rowListTasks = mysql_fetch_array($rsListTasks)){
				if($rowListTasks['task_assigned_to'] == $_SESSION['onlineMemberId']){
					$temp_url = BASE_URL . 'templates/' . $current_theme . '/images/';
					$rowListTasks['edit_bttn'] = '<a href="?act=project&id='.$_GET['id'].'&t='.$rowListTasks['task_id'].'"><img alt="Edit this task" src="'.$temp_url.'edit-icon.png" style="width: 16px; height: 16px;"></a>&nbsp;';
					$rowListTasks['del_bttn'] = '<a href="#" onclick="doRemoveTask('.$rowListTasks['task_id'].');"><img alt="Delete this task" src="'.$temp_url.'del-icon.png" style="width: 16px; height: 16px;"></a>&nbsp;';
				}
				$rowListTasks['iCountTask'] = $iCountTask;
				$iCountTask--;
				$xtpl->assign("ListTasks", $rowListTasks);
				$xtpl->parse("center.ListProjectWithTask.ListTasks");
			}
		}
		$xtpl->assign("ListProjectWithTask", $rowListProjectWithTask);
		$xtpl->parse("center.ListProjectWithTask");
		$xtpl->assign("projectId", $_GET['id']);
	}
?>
